package com.itheima.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.itheima.reggie.controller.EmployeeController;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.filter.LoginCheckFilter;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 源数据处理器
 */
@Component
@Slf4j
public class MyMetaObjecthandler implements MetaObjectHandler {

    /**
     * 思考如何动态获取  当前用户的ID
     * 通过ThreadLocal 来获取 Session对象
     * @param metaObject
     */

   //当我们执行保存的时候执行这个方法
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("公共字段自动填充----insert");
        log.info(metaObject.toString());
        //属性值（实体类对象的名称） + 一个值
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        Long aLong = BaseContext.getCurrentId();
        //修改人的ID
        metaObject.setValue("createUser", aLong);
        metaObject.setValue("updateUser", aLong);
    }
    //修改的时候执行这个方法
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info(metaObject.toString());
        log.info("公共字段自动填充----update");
        //修改时间  和修改人的ID
        Long aLong = BaseContext.getCurrentId();
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("updateUser",aLong);
    }
}
